<?php
namespace Admin\Controller;
class OrderRecycleController extends BaseController {

    public function index() {
        $map=[];
        if(I('get.is_confirm') === ""){
            $_GET['is_confirm'] = -1;   //全部
        }
        if(I('get.is_confirm') === "0" || I('get.is_confirm') ==="1"){
            $map['is_confirm'] = I('get.is_confirm');
        }
        if(I('get.type') === ""){
            $_GET['type'] = -1;   //全部
        }
        if(I('get.type') === "0" || I('get.type') ==="1"){
            $map['type'] = I('get.type');
        }
        if(!empty(I('get.time_start')) && empty(I('get.time_end'))){        //如果有【开始】时间
            $map['addtime'] = array("egt",strtotime(I('get.time_start')));
        }elseif(empty(I('get.time_start')) && !empty(I('get.time_end'))){   //如果有【结束】时间
            $map['addtime'] = array("elt",strtotime(I('get.time_end')));
        }elseif(!empty(I('get.time_start')) && !empty(I('get.time_end'))){  //如果有【开始】时间和【结束】时间
            $map['addtime'] = array('between',strtotime(I('get.time_start')).','.strtotime(I('get.time_end')));
        }


        $count = M('Recycle')
            ->alias('a')
            ->where($map)
            ->count();// 查询满足要求的总记录数
        $Page = new  \Admin\Common\Page($count , C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $this->assign('page' , $show);// 赋值分页输出
        $data = M('Recycle')->alias('a')      //            ->join('left join card b on a.user_id=b.user_id')
            ->where($map)
            ->join('left join book b on a.book_id=b.book_id')
            ->join('left join delivery_man c on a.man_id=c.man_id')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->field('a.*,b.sm,b.spbs,b.tm,b.dj,b.press,c.name')
            ->order('a.addtime desc')->select();
        if(!empty(I('get.excel')) && I('get.excel') == '1'){
            $data = M('Recycle')->alias('a')
            ->where($map)
                ->join('left join book b on a.book_id=b.book_id')
                ->join('left join delivery_man c on a.man_id=c.man_id')
                ->field('a.*,b.sm,b.spbs,b.tm,b.dj,b.press,c.name')
                ->order('a.addtime desc')->select();
            $file = excel_export($data);
            ouput_Browser($file);
        }else{
            $this->data = $data;
            $this->display();
        }
    }

    //确认回收
    function confirm() {
        $recycle_id = I("get.recycle_id/d");
        $r = M('Recycle')->where(['recycle_id'=>$recycle_id])->setField('is_confirm',1);
        if ($r) {
            $this->success('回收成功！');
        } else {
            $this->error('回收失败，请重试！');
        }
    }


}